//
//  MNDPlusDetailRootViewController.m
//  minidai
//
//  Created by Vitas on 2018/7/12.
//  Copyright © 2018年 迷你贷. All rights reserved.
//

#import "MNDPlusDetailRootViewController.h"
#import "UIImage+JKColor.h"
#import "MNDPlusInvestModel.h"
#import "MNDPlusTenderDetailViewController.h"

@interface MNDPlusDetailRootViewController ()
@property (strong, nonatomic) UIImage *naviBarImage;

@property(nonatomic, strong) MNDPlusInvestModel *investModel;

@property(nonatomic, strong) MNDPlusTenderDetailViewController *detailTopVC;
@property(nonatomic, strong) MNDPlusTenderDetailViewController *detailBottomVC;

@end

@implementation MNDPlusDetailRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.scrollEnabled = NO;
    if (kScreenHeight == 812) { //iPhone X
        self.tableView.tableHeaderView.height = 88;
    }
    
    if (@available(iOS 11, *)) {
        
    }else {
//        UIImage *image = [UIImage jk_imageWithColor:[UIColor colorWithRed:250/255.0 green:101/255.0 blue:66/255.0 alpha:1.0]];
//        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//        imageView.frame = CGRectMake(0, 0, kScreenWidth, 64);
//        self.tableView.tableHeaderView = imageView;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
//    self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@"backPic"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollNotificationAction:) name:@"scroll_up_or_down" object:nil];
    [self initBackButtonItemColorWhite:NO];
    
    [self loadData];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView scrollToTop];
    self.naviBarImage =  [UIImage jk_imageWithColor:[UIColor colorWithRed:250/255.0 green:101/255.0 blue:66/255.0 alpha:1.0]];
    [self.navigationController.navigationBar setBackgroundImage:self.naviBarImage forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    for (UIBarButtonItem *item in self.navigationItem.leftBarButtonItems) {
        [item setTintColor:[UIColor whiteColor]];
    }
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage jk_imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
    
    UIImage *image = [UIImage jk_imageWithColor:[UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:0.3]];
    self.navigationController.navigationBar.shadowImage = image;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
}

- (void)initBackButtonItemColorWhite:(BOOL)isWhite {

    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                target:nil action:nil];
    //将宽度设为负值
    spaceItem.width = kScreenWidth < 414? -10 : -15;
    
    UIImage *itemImage = [UIImage imageNamed:@"navi_back_btn_black"];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage: itemImage
                                                                 style: UIBarButtonItemStylePlain
                                                                target: self
                                                                action: @selector(backAction:)];
   
    
    [item setTintColor:kColorBlack];
    if (@available(iOS 11.0, *)) {
        self.navigationItem.leftBarButtonItem = item;
    } else {
        self.navigationItem.leftBarButtonItems = @[spaceItem, item];
    }
}

#pragma mark - Action

- (void)backAction:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)scrollNotificationAction:(NSNotification *)notification {
    NSInteger scrollType = [notification.object integerValue];
    if (scrollType == 1) {//详情
        [self.tableView scrollToRow:1 inSection:0 atScrollPosition:UITableViewScrollPositionNone animated:YES];
        self.naviBarImage = [UIImage new];
        [self.navigationController.navigationBar setBackgroundImage:self.naviBarImage forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
        self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:[UIColor colorWithRGB:0xF1F1F1]];
        
        for (UIBarButtonItem *item in self.navigationItem.leftBarButtonItems) {
            [item setTintColor:[UIColor blackColor]];
        }
        
    }else {//出借
        [self.tableView scrollToTop];
        self.naviBarImage =  [UIImage jk_imageWithColor:[UIColor colorWithRed:250/255.0 green:101/255.0 blue:66/255.0 alpha:1.0]];
        [self.navigationController.navigationBar setBackgroundImage:self.naviBarImage forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
        self.navigationController.navigationBar.shadowImage = [UIImage new];
        for (UIBarButtonItem *item in self.navigationItem.leftBarButtonItems) {
            [item setTintColor:[UIColor whiteColor]];
        }
//        [self.tableView scrollRectToVisible:CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight) animated:YES];
    }
    
}



- (void)loadData {
    [NetworkProgressHUD showHud];
    kdeclare_weakself;
    NSDictionary *params = [MNDSign signedParamWithDict:@{
                                                          @"plan_id":@(self.tenderModel.plan_id)
                                                          }];
    [APIManager sendRequestWithURL:API_AUTO_PLUS_DETAIL parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        [NetworkProgressHUD hideHud];
        if (responseModel.status == ResponseSuccess) {
            weakSelf.investModel = [MNDPlusInvestModel mj_objectWithKeyValues:responseModel.data];
            weakSelf.detailTopVC.investModel = weakSelf.investModel;
            weakSelf.detailBottomVC.investModel = weakSelf.investModel;
        }else {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
        
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    
    if (kScreenHeight >= 812) {
        
        //iphoneX要减掉导航条的高度
        return screenHeight - 88;
    } else {
        //要减掉导航条的高度
        return screenHeight - 64;
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"firstController"]) {
        self.detailTopVC = [segue destinationViewController];
        }
    if ([segue.identifier isEqualToString:@"secondController"]) {
        self.detailBottomVC = [segue destinationViewController];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
